import O1HD, { managers, modules, services } from '../../engines/gameEngine';
import node_component from '../../engines/libs/node_component';
import PageBase from '../Base/PageBase';
import { GAME_EVENT } from '../Enum';
import { INormalLevelStruct, ITipsPagePrams } from '../Interface';
import mainRoot from '../MainRoot';
import tipsPageSpNode from '../Obj/tipsPageSpNode';

const { ccclass, property, menu, disallowMultiple } = cc._decorator;

@ccclass
@disallowMultiple
@menu('游戏页面/提示页面')
export default class TipsPage extends PageBase {
    @property({ type: cc.Label, displayName: "提示展示" })
    private tipsShow: cc.Label = null;
    @property({ type: cc.Sprite, displayName: "提示加载的图片" })
    private img: cc.Sprite = null;
    @property({ type: cc.Node, displayName: "底板框" })
    private boardFrame: cc.Node = null;
    @property({ type: cc.Node, displayName: "领取按钮" })
    private rewardBtn: cc.Node = null;

    @property({ type: cc.Node, displayName: "提示" })
    private tipNode: cc.Node = null;

    @property({ type: cc.Node, displayName: "答案" })
    private answerNode: cc.Node = null;

    @property({type:cc.Node,displayName:"spScaleNode"})
    spScaleNode:cc.Node = null;

    tipsAndAnswer: string = "";

    autoShowSp:boolean = false;

    tipsAndAnswerLength: number = 0;
    async OnPageOpen(prams: ITipsPagePrams) {
        super.OnPageOpen(prams);
        let tipsStr: string = prams.tips;
        let tipAndAnswer: string[] = tipsStr.split("#");
        this.tipsAndAnswer = tipAndAnswer[1];
        this.tipsAndAnswerLength = tipAndAnswer[0].length;
        let tipsType = tipAndAnswer[0].includes("$h");
        tipAndAnswer[0] = tipAndAnswer[0].replace("$h","");
        let tipFontLength = 10;
        let tipNode:cc.Node = this.tipNode.getChildByName("lbSmallLayout");
        if(tipsType){
            tipNode = this.tipNode.getChildByName("lbHLayout");
        }else if(tipAndAnswer[0].length>40){
            tipNode = this.tipNode.getChildByName("lbBigLayout");
            tipFontLength = 16;
        }

        if (tipAndAnswer[0].indexOf("/") > -1) {
            const sp = this.tipNode.getChildByName("mask").getChildByName("sp");
            sp.active = true;
            for (let i = 0; i < tipNode.childrenCount; i++) {
                let aLabel = tipNode.getChildByName("Label" + i);
                aLabel.active = false;
            }
            cc.resources.load(this.tipsAndAnswer, cc.SpriteFrame, (err, asset: cc.SpriteFrame) => {
                if (err) {
                    console.log("加载提示图片出错");
                    return;
                }
                sp.getComponent(cc.Sprite).spriteFrame = asset;

                let width = asset.getTexture().width;
                let height = asset.getTexture().height;

                let wRadio:number = 208/width;
                let hRadio:number = 440/height;

                let radio = 1;
                if(wRadio>hRadio){
                    radio = wRadio;
                }else{
                    radio = hRadio;
                }

                sp.width = sp.width*radio;
                sp.height = sp.height*radio;

            });
        }else{
            tipNode.active = true;
            if(tipsType){
                tipNode.children[0].getComponent(cc.Label).string = tipAndAnswer[0];
            }else{
                let needHang2: number = Math.ceil(tipAndAnswer[0].length / tipFontLength);
                for (let i = 0; i < tipNode.childrenCount; i++) {
                    let aLabel = tipNode.getChildByName("Label" + i);
                    aLabel.active = i < needHang2;
                    let answerStr: string = "";
                    for (let j = i * tipFontLength; j < i * tipFontLength + tipFontLength; j++) {
                        if (tipAndAnswer[0][j])
                            answerStr += tipAndAnswer[0][j];
                    }
        
                    aLabel.getComponent(cc.Label).string = answerStr;
                    if(i>=needHang2){
                        aLabel.active = false;
                    }else{
                        aLabel.active = true;
                    }
                }
            }
        }


        // this.tipNode.getChildByName("Label").getComponent(cc.Label).string = tipAndAnswer[0];

        // let answerType = tipAndAnswer[1].includes("$h");
        // tipAndAnswer[1] = tipAndAnswer[1].replace("$h","");
        // this.autoShowSp = tipAndAnswer[1].includes("!");
        // tipAndAnswer[1] = tipAndAnswer[1].replace("!","");
        // let answerNode = this.answerNode.getChildByName("lbSmallLayout");
        // let answerFontLength = 10;
        // if(answerType){
        //     answerNode = this.answerNode.getChildByName("lbHLayout")
        // }else if(tipAndAnswer[1].length>40) {
        //     answerNode = this.answerNode.getChildByName("lbBigLayout");
        //     answerFontLength = 16;
        // }
        // if (tipAndAnswer[1].indexOf("/") > -1) {
        //     this.answerNode.getComponent(cc.Button).interactable = false;
        //     const sp = this.answerNode.getChildByName("mask").getChildByName("sp");
        //     sp.active = true;
        //     for (let i = 0; i < answerNode.childrenCount; i++) {
        //         let aLabel = answerNode.getChildByName("Label" + i);
        //         aLabel.active = false;
        //     }
        //     cc.resources.load(tipAndAnswer[1], cc.SpriteFrame, (err, asset: cc.SpriteFrame) => {
        //         if (err) {
        //             console.log("加载提示图片出错");
        //             return;
        //         }
        //         sp.getComponent(cc.Sprite).spriteFrame = asset;

        //         let width = asset.getTexture().width;
        //         let height = asset.getTexture().height;

        //         let wRadio:number = 208/width;
        //         let hRadio:number = 440/height;



        //         let radio = 1;
        //         if(wRadio>hRadio){
        //             radio = wRadio;
        //         }else{
        //             radio = hRadio;
        //         }

        //         sp.width = sp.width*radio;
        //         sp.height = sp.height*radio;
                
        //     });
        // } else {
        //     answerNode.active = true;
        //     if(answerType){
        //         answerNode.children[0].getComponent(cc.Label).string = tipAndAnswer[1];
        //     }else{
        //         let needHang: number = Math.ceil(tipAndAnswer[1].length / answerFontLength);
        //         for (let i = 0; i < answerNode.childrenCount; i++) {
        //             let aLabel = answerNode.getChildByName("Label" + i);
        //             aLabel.active = i < needHang;
        //             let answerStr: string = "";
        //             for (let j = i * answerFontLength; j < i * answerFontLength + answerFontLength; j++) {
        //                 if (tipAndAnswer[1][j])
        //                     answerStr += tipAndAnswer[1][j];
        //             }
    
        //             aLabel.getComponent(cc.Label).string = answerStr;
        //         }
        //     }

        // }


        // let level = mainRoot.LevelSystem.GetCurrLevel(this.m_mode);
        // const content: INormalLevelStruct = mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode, level);
        // if(content.name == "Level_66"){

        // }

        // console.warn('获取1', mainRoot.GotTips);
        this.node.getChildByName("Content").getChildByName("ad1").active = false; //!mainRoot.GotTips;
        // console.error(mainRoot.GotTips)
        // this.tipNode.opacity = mainRoot.GotTips ? 255 : 0;
        this.node.getChildByName("Content").getChildByName("ad3").active = false; //!mainRoot.GotAnswer && !mainRoot.GotTips;
        this.node.getChildByName("Content").getChildByName("ad2").active =  false; // !mainRoot.GotAnswer && mainRoot.GotTips;
        // this.answerNode.opacity = mainRoot.GotAnswer ? 255 : 0;
        O1HD.emit(GAME_EVENT.GAME_PAUSE);
    }

    onClickTip() {

        cc.game.pause();
        services().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(() => {
            this.tipNode.opacity = 255;
            this.node.getChildByName("Content").getChildByName("ad1").active = false;
            this.node.getChildByName("Content").getChildByName("ad3").active = false;
            this.node.getChildByName("Content").getChildByName("ad2").active = true;
            O1HD.emit(GAME_EVENT.ON_TIPS_GOT);
            mainRoot.GotTips = true;
        }, () => {
            cc.game.resume();
        }, () => {
            cc.game.resume();
        }, "看视频获取提示");
    }

    onClickAnswer() {
        if (this.tipNode.opacity != 255) {
            modules().uiControlModule.showMessageBox("请先解锁提示");
            return;
        }
        if(mainRoot.GotAnswer){
            return;
        }
        cc.game.pause();
        services().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(() => {
            let level = mainRoot.LevelSystem.GetCurrLevel(this.m_mode);
            const content: INormalLevelStruct = mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode, level);
            this.answerNode.opacity = 255;
            this.node.getChildByName("Content").getChildByName("ad2").active = false;
            O1HD.emit(GAME_EVENT.ON_ANSWER_GOT);
            mainRoot.GotAnswer = true;
            O1HD.emit('level-to-hint');
            if(this.autoShowSp){
                this.onShowSp(null,"answer");
            }
        }, () => {
            cc.game.resume();
        }, () => {
            cc.game.resume();
        }, "看视频获取答案");
    }

    OnRewardBtn() {
        PageBase.isPlaySound = false;
        O1HD.emit(GAME_EVENT.GAME_RESUME);
        managers().soundMgr.playSound("mainSound/anNiu");
        this.onReward && this.onReward();
        this.ClosePage();
    }

    onShowSp(e:cc.Event.EventTouch,type:string){
        let spTexture :cc.SpriteFrame = null;
        switch(type){
            case "tip":{
                spTexture = this.tipNode.getChildByName("mask").getChildByName("sp").getComponent(cc.Sprite).spriteFrame.clone();
                break;
            }
            case "answer":{
                let sp = this.answerNode.getChildByName("mask").getChildByName("sp").getComponent(cc.Sprite).spriteFrame;
                if(sp)
                spTexture = sp.clone();
                break;
            }
        }
        if(spTexture){
            this.spScaleNode.active = true;
            this.spScaleNode.opacity = 0;
            let spNode = this.spScaleNode.getChildByName("sp");
            spNode.setPosition(cc.v2(0,0));
            spNode.getComponent(cc.Sprite).spriteFrame = spTexture;
            let width = spTexture.getTexture().width;
            let height = spTexture.getTexture().height;
            let canvasWidth = this.spScaleNode.width;
            let radio = canvasWidth/width;
            spNode.width = width*radio;
            spNode.height = height*radio;
            this.spScaleNode.getChildByName("bg").active = true;
            cc.tween(this.spScaleNode).to(0.5,{opacity:255}).start();
        }
    }

    closeSpScale(){
        this.spScaleNode.active = false;
    }

    @property({type:cc.Node,displayName:"spScaleNode"})
    answerScaleNode:cc.Node = null;

    answerNodeCopy:cc.Node = null;

    onAnswerClick(){
        this.answerNodeCopy = cc.instantiate(this.answerNode);
        this.answerScaleNode.addChild(this.answerNodeCopy);
        this.answerNodeCopy.scale = 2;
        let ts = this.answerNodeCopy.addComponent(tipsPageSpNode);
        this.answerNodeCopy.removeComponent(cc.Button);
        ts.tipsPage = this;
        ts.scaleMul = 0.2;
        ts.maxScale = 3;
        this.answerScaleNode.active = true;
        this.answerNodeCopy.setPosition(cc.v2(0,0));
        this.answerScaleNode.getChildByName("bg").active = true;
        cc.tween(this.answerScaleNode).to(0.5,{opacity:255}).start();
    }

    closeAnswerScale(){
        this.answerNodeCopy&&this.answerNodeCopy?.destroy();
        this.answerNodeCopy&&this.answerNodeCopy?.removeFromParent();
        this.answerNodeCopy = null;
        this.answerScaleNode.active = false;
    }
}